using System;
using System.Diagnostics;
using System.Reflection;

namespace TrueCode.Core.Diagnostics
{
	partial class Diagnostic
	{
		private class Tracer : IDisposable
		{
			#region Fields
			private readonly Diagnostic	_owner;
			private readonly MethodBase _method;
			private readonly String		_senderInfo;
			private readonly String		_subsystem;
			private readonly Object[]	_parameters;
			#endregion

			#region Properties
			public MethodBase Method
			{
				get { return _method; }
			}

			public String SenderInfo
			{
				get { return _senderInfo; }
			}

			public string Subsystem
			{
				get { return _subsystem; }
			}

			public Object[ ] Parameters
			{
				get { return _parameters; }
			}
			#endregion

			#region .ctors
			public Tracer( Diagnostic owner, String senderInfo, String subsystem, params Object[] parameters )
			{
				_owner			= owner;
				_senderInfo	= senderInfo;
				_subsystem		= subsystem;
				_parameters	= parameters;

				var stackTrace = new StackTrace( false );
				_method = stackTrace.GetFrame( 2 ).GetMethod( );

				_owner.WriteTraceEnter( this );
			}
			#endregion

			#region IDisposable Members
			void IDisposable.Dispose( )
			{
				_owner.WriteTraceExit( this );
			}
			#endregion
		}
	}
}